        <!DOCTYPE html>
        <html>
        <head>
                <meta charset="utf-8">
        <title>Distance class / box2d_html Library / Dart Documentation</title>
        <link rel="stylesheet" type="text/css"
            href="../styles.css">
        <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800" rel="stylesheet" type="text/css">
        <link rel="shortcut icon" href="../favicon.ico">
        
        </head>
        <body data-library="box2d_html" data-type="Distance">
        <div class="page">
        <div class="header">
          <a href="../index.html"><div class="logo"></div></a>
          <a href="../index.html">Dart Documentation</a>
         &rsaquo; <a href="../box2d_html.html">box2d_html</a> &rsaquo; <a href="../box2d_html/Distance.html">Distance</a>        <div id="search-box">
          <input type="search" name="q" id="q" autocomplete="off"
              class="search-input" placeholder="Search API">
        </div>
        
      </div>
      <div class="drop-down" id="drop-down"></div>
      
        <div class="nav">
        
</div>
<div class="content">
        <h2><strong>Distance</strong>
          class
        </h2>
        
<button id="show-inherited" class="show-inherited">Hide inherited</button>
<div class="doc">
<p>This is non-static for faster pooling. To get an instance,
use the SingletonPool rather than construct a distance object.</p>
<pre class="source">
class Distance {
  int calls;
  int iters;
  int maxIters;

  /** Pool variables for use in distance calculation. */
  Simplex simplex;
  List&lt;int&gt; saveA;
  List&lt;int&gt; saveB;
  Vector closestPoint;
  Vector searchDirection;
  Vector temp;
  Vector normal;

  /**
   * Construct a new Distance object. For internal use only. Don't directly
   * invoke.
   */
  Distance._construct() :
    simplex = new Simplex(),
    saveA = new List&lt;int&gt;(3),
    saveB = new List&lt;int&gt;(3),
    closestPoint = new Vector(),
    searchDirection = new Vector(),
    temp = new Vector(),
    normal = new Vector(),
    calls = 0,
    iters = 0,
    maxIters = 20 { }

  /**
   * Compute the closest points between two shapes. Supports any combination of:
   * CircleShape and PolygonShape. The simplex cache is input/output.
   * On the first call set SimplexCache.count to zero.
   */
  void distance(DistanceOutput output, SimplexCache cache,
      DistanceInput input) {
    calls++;

    final DistanceProxy proxyA = input.proxyA;
    final DistanceProxy proxyB = input.proxyB;

    Transform transformA = input.transformA;
    Transform transformB = input.transformB;

    // Initialize the simplex.
    simplex.readCache(cache, proxyA, transformA, proxyB, transformB);

    // Get simplex vertices as an array.
    List&lt;SimplexVertex&gt; vertices = simplex.vertices;

    // These store the vertices of the last simplex so that we
    // can check for duplicates and prevent cycling.
    // (pooled above)
    int saveCount = 0;

    simplex.getClosestPoint(closestPoint);
    num distanceSqr1 = closestPoint.lengthSquared;
    num distanceSqr2 = distanceSqr1;

    // Main iteration loop
    int iter = 0;
    while (iter &lt; maxIters) {

      // Copy simplex so we can identify duplicates.
      saveCount = simplex.count;
      for (int i = 0; i &lt; saveCount; i++) {
        saveA[i] = vertices[i].indexA;
        saveB[i] = vertices[i].indexB;
      }

      switch (simplex.count) {
        case 1 :
          break;
        case 2 :
          simplex.solve2();
          break;
        case 3 :
          simplex.solve3();
          break;
        default :
          assert (false);
      }

      // If we have 3 points, then the origin is in the corresponding triangle.
      if (simplex.count == 3)
        break;

      // Compute closest point.
      simplex.getClosestPoint(closestPoint);
      distanceSqr2 = closestPoint.lengthSquared;

      distanceSqr1 = distanceSqr2;

      // get search direction;
      simplex.getSearchDirection(searchDirection);

      // Ensure the search direction is numerically fit.
      if (searchDirection.lengthSquared &lt; Settings.EPSILON * Settings.EPSILON) {
        // The origin is probably contained by a line segment
        // or triangle. Thus the shapes are overlapped.

        // We can't return zero here even though there may be overlap.
        // In case the simplex is a point, segment, or triangle it is difficult
        // to determine if the origin is contained in the CSO or very close to
        // it.
        break;
      }

      // Compute a tentative new simplex vertex using support points.
      SimplexVertex vertex = vertices[simplex.count];

      Matrix22.mulTransMatrixAndVectorToOut(transformA.rotation,
          searchDirection.negateLocal(), temp);
      vertex.indexA = proxyA.getSupport(temp);
      Transform.mulToOut(transformA, proxyA.vertices[vertex.indexA],
          vertex.wA);
      // Vec2 wBLocal;
      Matrix22.mulTransMatrixAndVectorToOut(transformB.rotation,
          searchDirection.negateLocal(), temp);
      vertex.indexB = proxyB.getSupport(temp);
      Transform.mulToOut(transformB, proxyB.vertices[vertex.indexB],
          vertex.wB);
      vertex.w.setFrom(vertex.wB).subLocal(vertex.wA);

      // Iteration count is equated to the number of support point calls.
      ++iter;
      ++iters;

      // Check for duplicate support points. This is the main termination
      // criteria.
      bool duplicate = false;
      for (int i = 0; i &lt; saveCount; ++i) {
        if (vertex.indexA == saveA[i] &amp;&amp; vertex.indexB == saveB[i]) {
          duplicate = true;
          break;
        }
      }

      // If we found a duplicate support point we must exit to avoid cycling.
      if (duplicate)
        break;

      // New vertex is ok and needed.
      ++simplex.count;
    }

    maxIters = Math.max(maxIters, iter);

    // Prepare output.
    simplex.getWitnessPoints(output.pointA, output.pointB);
    output.distance = MathBox.distance(output.pointA, output.pointB);
    output.iterations = iter;

    // Cache the simplex.
    simplex.writeCache(cache);

    // Apply radii if requested.
    if (input.useRadii) {
      num rA = proxyA.radius;
      num rB = proxyB.radius;

      if (output.distance &gt; rA + rB &amp;&amp; output.distance &gt; Settings.EPSILON) {
        // Shapes are still no overlapped.
        // Move the witness points to the outer surface.
        output.distance -= rA + rB;
        normal.setFrom(output.pointB).subLocal(output.pointA);
        normal.normalize();
        temp.setFrom(normal).mulLocal(rA);
        output.pointA.addLocal(temp);
        temp.setFrom(normal).mulLocal(rB);
        output.pointB.subLocal(temp);
      } else {
        // Shapes are overlapped when radii are considered.
        // Move the witness points to the middle.
        output.pointA.addLocal(output.pointB).mulLocal(.5);
        output.pointB.setFrom(output.pointA);
        output.distance = 0.0;
      }
    }
  }
}
</pre>
</div>
<div>
<h3>Constructors</h3>
<div class="method"><h4 id="Distance._construct">
<button class="show-code">Code</button>
new <strong>Distance._construct</strong>() <a class="anchor-link" href="#Distance._construct"
              title="Permalink to Distance.Distance._construct">#</a></h4>
<div class="doc">
<p>Construct a new Distance object. For internal use only. Don't directly
invoke.</p>
<pre class="source">
Distance._construct() :
  simplex = new Simplex(),
  saveA = new List&lt;int&gt;(3),
  saveB = new List&lt;int&gt;(3),
  closestPoint = new Vector(),
  searchDirection = new Vector(),
  temp = new Vector(),
  normal = new Vector(),
  calls = 0,
  iters = 0,
  maxIters = 20 { }
</pre>
</div>
</div>
</div>
<div>
<h3>Properties</h3>
<div class="field"><h4 id="calls">
<button class="show-code">Code</button>
int         <strong>calls</strong> <a class="anchor-link"
            href="#calls"
            title="Permalink to Distance.calls">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int calls;
</pre>
</div>
</div>
<div class="field"><h4 id="closestPoint">
<button class="show-code">Code</button>
<a href="../box2d_html/Vector.html">Vector</a>         <strong>closestPoint</strong> <a class="anchor-link"
            href="#closestPoint"
            title="Permalink to Distance.closestPoint">#</a>
        </h4>
        <div class="doc">
<pre class="source">
Vector closestPoint;
</pre>
</div>
</div>
<div class="field"><h4 id="iters">
<button class="show-code">Code</button>
int         <strong>iters</strong> <a class="anchor-link"
            href="#iters"
            title="Permalink to Distance.iters">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int iters;
</pre>
</div>
</div>
<div class="field"><h4 id="maxIters">
<button class="show-code">Code</button>
int         <strong>maxIters</strong> <a class="anchor-link"
            href="#maxIters"
            title="Permalink to Distance.maxIters">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int maxIters;
</pre>
</div>
</div>
<div class="field"><h4 id="normal">
<button class="show-code">Code</button>
<a href="../box2d_html/Vector.html">Vector</a>         <strong>normal</strong> <a class="anchor-link"
            href="#normal"
            title="Permalink to Distance.normal">#</a>
        </h4>
        <div class="doc">
<pre class="source">
Vector normal;
</pre>
</div>
</div>
<div class="field inherited"><h4 id="runtimeType">
<button class="show-code">Code</button>
final Type         <strong>runtimeType</strong> <a class="anchor-link"
            href="#runtimeType"
            title="Permalink to Distance.runtimeType">#</a>
        </h4>
        <div class="inherited-from">inherited from Object </div><div class="doc">
<p>A representation of the runtime type of the object.</p>
<pre class="source">
external Type get runtimeType;
</pre>
</div>
</div>
<div class="field"><h4 id="saveA">
<button class="show-code">Code</button>
List&lt;int&gt;         <strong>saveA</strong> <a class="anchor-link"
            href="#saveA"
            title="Permalink to Distance.saveA">#</a>
        </h4>
        <div class="doc">
<pre class="source">
List&lt;int&gt; saveA;
</pre>
</div>
</div>
<div class="field"><h4 id="saveB">
<button class="show-code">Code</button>
List&lt;int&gt;         <strong>saveB</strong> <a class="anchor-link"
            href="#saveB"
            title="Permalink to Distance.saveB">#</a>
        </h4>
        <div class="doc">
<pre class="source">
List&lt;int&gt; saveB;
</pre>
</div>
</div>
<div class="field"><h4 id="searchDirection">
<button class="show-code">Code</button>
<a href="../box2d_html/Vector.html">Vector</a>         <strong>searchDirection</strong> <a class="anchor-link"
            href="#searchDirection"
            title="Permalink to Distance.searchDirection">#</a>
        </h4>
        <div class="doc">
<pre class="source">
Vector searchDirection;
</pre>
</div>
</div>
<div class="field"><h4 id="simplex">
<button class="show-code">Code</button>
<a href="../box2d_html/Simplex.html">Simplex</a>         <strong>simplex</strong> <a class="anchor-link"
            href="#simplex"
            title="Permalink to Distance.simplex">#</a>
        </h4>
        <div class="doc">
<p>Pool variables for use in distance calculation.</p>
<pre class="source">
Simplex simplex;
</pre>
</div>
</div>
<div class="field"><h4 id="temp">
<button class="show-code">Code</button>
<a href="../box2d_html/Vector.html">Vector</a>         <strong>temp</strong> <a class="anchor-link"
            href="#temp"
            title="Permalink to Distance.temp">#</a>
        </h4>
        <div class="doc">
<pre class="source">
Vector temp;
</pre>
</div>
</div>
</div>
<div>
<h3>Operators</h3>
<div class="method inherited"><h4 id="==">
<button class="show-code">Code</button>
bool <strong>operator ==</strong>(other) <a class="anchor-link" href="#=="
              title="Permalink to Distance.operator ==">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>The equality operator.</p>
<p>The default behavior for all <code>Object</code>s is to return true if and
only if <code>this</code> and 
<span class="param">other</span> are the same object.</p>
<p>If a subclass overrides the equality operator it should override
the <code>hashCode</code> method as well to maintain consistency.</p>
<pre class="source">
bool operator ==(other) =&gt; identical(this, other);
</pre>
</div>
</div>
</div>
<div>
<h3>Methods</h3>
<div class="method"><h4 id="distance">
<button class="show-code">Code</button>
void <strong>distance</strong>(<a href="../box2d_html/DistanceOutput.html">DistanceOutput</a> output, <a href="../box2d_html/SimplexCache.html">SimplexCache</a> cache, <a href="../box2d_html/DistanceInput.html">DistanceInput</a> input) <a class="anchor-link" href="#distance"
              title="Permalink to Distance.distance">#</a></h4>
<div class="doc">
<p>Compute the closest points between two shapes. Supports any combination of:
CircleShape and PolygonShape. The simplex cache is input/output.
On the first call set SimplexCache.count to zero.</p>
<pre class="source">
void distance(DistanceOutput output, SimplexCache cache,
    DistanceInput input) {
  calls++;

  final DistanceProxy proxyA = input.proxyA;
  final DistanceProxy proxyB = input.proxyB;

  Transform transformA = input.transformA;
  Transform transformB = input.transformB;

  // Initialize the simplex.
  simplex.readCache(cache, proxyA, transformA, proxyB, transformB);

  // Get simplex vertices as an array.
  List&lt;SimplexVertex&gt; vertices = simplex.vertices;

  // These store the vertices of the last simplex so that we
  // can check for duplicates and prevent cycling.
  // (pooled above)
  int saveCount = 0;

  simplex.getClosestPoint(closestPoint);
  num distanceSqr1 = closestPoint.lengthSquared;
  num distanceSqr2 = distanceSqr1;

  // Main iteration loop
  int iter = 0;
  while (iter &lt; maxIters) {

    // Copy simplex so we can identify duplicates.
    saveCount = simplex.count;
    for (int i = 0; i &lt; saveCount; i++) {
      saveA[i] = vertices[i].indexA;
      saveB[i] = vertices[i].indexB;
    }

    switch (simplex.count) {
      case 1 :
        break;
      case 2 :
        simplex.solve2();
        break;
      case 3 :
        simplex.solve3();
        break;
      default :
        assert (false);
    }

    // If we have 3 points, then the origin is in the corresponding triangle.
    if (simplex.count == 3)
      break;

    // Compute closest point.
    simplex.getClosestPoint(closestPoint);
    distanceSqr2 = closestPoint.lengthSquared;

    distanceSqr1 = distanceSqr2;

    // get search direction;
    simplex.getSearchDirection(searchDirection);

    // Ensure the search direction is numerically fit.
    if (searchDirection.lengthSquared &lt; Settings.EPSILON * Settings.EPSILON) {
      // The origin is probably contained by a line segment
      // or triangle. Thus the shapes are overlapped.

      // We can't return zero here even though there may be overlap.
      // In case the simplex is a point, segment, or triangle it is difficult
      // to determine if the origin is contained in the CSO or very close to
      // it.
      break;
    }

    // Compute a tentative new simplex vertex using support points.
    SimplexVertex vertex = vertices[simplex.count];

    Matrix22.mulTransMatrixAndVectorToOut(transformA.rotation,
        searchDirection.negateLocal(), temp);
    vertex.indexA = proxyA.getSupport(temp);
    Transform.mulToOut(transformA, proxyA.vertices[vertex.indexA],
        vertex.wA);
    // Vec2 wBLocal;
    Matrix22.mulTransMatrixAndVectorToOut(transformB.rotation,
        searchDirection.negateLocal(), temp);
    vertex.indexB = proxyB.getSupport(temp);
    Transform.mulToOut(transformB, proxyB.vertices[vertex.indexB],
        vertex.wB);
    vertex.w.setFrom(vertex.wB).subLocal(vertex.wA);

    // Iteration count is equated to the number of support point calls.
    ++iter;
    ++iters;

    // Check for duplicate support points. This is the main termination
    // criteria.
    bool duplicate = false;
    for (int i = 0; i &lt; saveCount; ++i) {
      if (vertex.indexA == saveA[i] &amp;&amp; vertex.indexB == saveB[i]) {
        duplicate = true;
        break;
      }
    }

    // If we found a duplicate support point we must exit to avoid cycling.
    if (duplicate)
      break;

    // New vertex is ok and needed.
    ++simplex.count;
  }

  maxIters = Math.max(maxIters, iter);

  // Prepare output.
  simplex.getWitnessPoints(output.pointA, output.pointB);
  output.distance = MathBox.distance(output.pointA, output.pointB);
  output.iterations = iter;

  // Cache the simplex.
  simplex.writeCache(cache);

  // Apply radii if requested.
  if (input.useRadii) {
    num rA = proxyA.radius;
    num rB = proxyB.radius;

    if (output.distance &gt; rA + rB &amp;&amp; output.distance &gt; Settings.EPSILON) {
      // Shapes are still no overlapped.
      // Move the witness points to the outer surface.
      output.distance -= rA + rB;
      normal.setFrom(output.pointB).subLocal(output.pointA);
      normal.normalize();
      temp.setFrom(normal).mulLocal(rA);
      output.pointA.addLocal(temp);
      temp.setFrom(normal).mulLocal(rB);
      output.pointB.subLocal(temp);
    } else {
      // Shapes are overlapped when radii are considered.
      // Move the witness points to the middle.
      output.pointA.addLocal(output.pointB).mulLocal(.5);
      output.pointB.setFrom(output.pointA);
      output.distance = 0.0;
    }
  }
}
</pre>
</div>
</div>
<div class="method"><h4 id="Distance._construct">
<button class="show-code">Code</button>
new <strong>Distance._construct</strong>() <a class="anchor-link" href="#Distance._construct"
              title="Permalink to Distance.Distance._construct">#</a></h4>
<div class="doc">
<p>Construct a new Distance object. For internal use only. Don't directly
invoke.</p>
<pre class="source">
Distance._construct() :
  simplex = new Simplex(),
  saveA = new List&lt;int&gt;(3),
  saveB = new List&lt;int&gt;(3),
  closestPoint = new Vector(),
  searchDirection = new Vector(),
  temp = new Vector(),
  normal = new Vector(),
  calls = 0,
  iters = 0,
  maxIters = 20 { }
</pre>
</div>
</div>
<div class="method inherited"><h4 id="hashCode">
<button class="show-code">Code</button>
int <strong>hashCode</strong>() <a class="anchor-link" href="#hashCode"
              title="Permalink to Distance.hashCode">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Get a hash code for this object.</p>
<p>All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator <code>==</code>. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.</p>
<p>If a subclass overrides <code>hashCode</code> it should override the
equality operator as well to maintain consistency.</p>
<pre class="source">
external int hashCode();
</pre>
</div>
</div>
<div class="method inherited"><h4 id="noSuchMethod">
<button class="show-code">Code</button>
<strong>noSuchMethod</strong>(String name, List args) <a class="anchor-link" href="#noSuchMethod"
              title="Permalink to Distance.noSuchMethod">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p><code>noSuchMethod</code> is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to <code>noSuchMethod</code>. If <code>noSuchMethod</code>
returns a value, that value becomes the result of the original
invocation.</p>
<p>The default behavior of <code>noSuchMethod</code> is to throw a
<code>noSuchMethodError</code>.</p>
<pre class="source">
external Dynamic noSuchMethod(String name, List args);
</pre>
</div>
</div>
<div class="method inherited"><h4 id="Object">
<button class="show-code">Code</button>
const <strong>Object</strong>() <a class="anchor-link" href="#Object"
              title="Permalink to Distance.Object">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Creates a new <code>Object</code> instance.</p>
<p><code>Object</code> instances have no meaningful state, and are only useful
through their identity. An <code>Object</code> instance is equal to itself
only.</p>
<pre class="source">
const Object();
</pre>
</div>
</div>
<div class="method inherited"><h4 id="toString">
<button class="show-code">Code</button>
String <strong>toString</strong>() <a class="anchor-link" href="#toString"
              title="Permalink to Distance.toString">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Returns a string representation of this object.</p>
<pre class="source">
external String toString();
</pre>
</div>
</div>
</div>
        </div>
        <div class="clear"></div>
        </div>
        
        <div class="footer">
          
        </div>
        <script async src="../client-live-nav.js"></script>
        </body></html>
        
